package com.iseastar.dianxiaosan.home.parcel;

import java.util.ArrayList;

import android.os.Bundle;
import android.os.Message;
import android.support.v4.widget.SwipeRefreshLayout.OnRefreshListener;
import android.widget.TextView;

import com.iseastar.BaseActivity2;
import com.iseastar.dianxiaosan.home.adapter.ParcelOrderedAdapter;
import com.iseastar.dianxiaosan.model.ParcelOrderedBean;
import com.iseastar.guojiang.BaseActivity;
import com.iseastar.guojiang.app.AppCache;
import com.iseastar.guojiang.consts.MsgID;
import com.iseastar.guojiang.logic.AppHttp;
import com.iseastar.guojiang.logic.JSON;
import com.iseastar.guojiang.model.ReqResult;
import com.iseastar.guojiang.model.UserBean;
import com.shouhuobao.station.R;

import droid.frame.view.SwipeListViewOnScrollListener;
import droid.frame.view.xlist.SListView;

/**
 * 用户已下单
 * 
 * @author kaka <br>
 *         2017年3月28日
 */
public class ParcelOrderedListActivity extends BaseActivity2 {
	private SListView mListView;
	private ParcelOrderedAdapter mAdapter;
	private TextView parcelOrderedNum;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		UserBean user = AppCache.getUser();
		AppHttp.getInstance().stationParcelOrderedAlls(user.getStationId());
	}

	@Override
	protected void findViewById() {
		setContentView(R.layout.parcel_ordered);
		super.findViewById();
		getAppTitle().setCommonTitle("用户已下单");

		mListView = (SListView) findViewById(R.id.listview);
		parcelOrderedNum = (TextView) findViewById(R.id.parcel_ordered_num);
		mListView.setFooterDividersEnabled(true);

		mSwipeLayout.setOnRefreshListener(new OnRefreshListener() {
			@Override
			public void onRefresh() {
				UserBean user = AppCache.getUser();
				AppHttp.getInstance().stationParcelOrderedAlls(user.getStationId());
			}
		});
		//
		mListView.setOnScrollListener(new SwipeListViewOnScrollListener(mSwipeLayout));
	}

	@Override
	public boolean handleMessage(Message msg) {
		switch (msg.what) {
		// 获取驿站点下面的订单列表（待收取的包裹）
		case MsgID.ordered_take_alls:
			onRefreshComplete();
			final ReqResult<ParcelOrderedBean> parseList = JSON.parseList(msg.obj, ParcelOrderedBean.class);
			// final ReqResult<ParcelWaitResultBean> result = JSON.parser(msg.obj, ParcelWaitResultBean.class);
			if (checkLoginStatus(parseList) && parseList.getResult() != null) {
				runOnUiThread(new Runnable() {
					@Override
					public void run() {
						ArrayList<ParcelOrderedBean> resultList = parseList.getResultList();
						// ParcelWaitResultBean items = result.getResult();
						mAdapter = new ParcelOrderedAdapter(resultList, getContext(), mListView);
						mListView.setAdapter(mAdapter);
						parcelOrderedNum.setText("共" + mAdapter.getItemsSize() + "个包裹");
					}
				});
			} else {
				showToast(parseList.getMessage());
			}
			return true;
		default:
			return super.handleMessage(msg);
		}
	}
}
